<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class Insertion extends Sort
{

    function __construct($SourceData)
    {
        parent::__construct($SourceData);
        $this->name = "插入排序";
        $this->realName = "insertion";

    }

    function __toString()
    {
        return "insertion";
    }

    function Sort($index)
    {
        for ($i = 0; $i < $this->testNumber - 1; $i++) //注意到边界所以-1
        {
            if ($this->data[$index][$i] > $this->data[$index][$i + 1])//2,3
            {
                $temp = $this->data[$index][$i + 1]; //3
                $this->moveCount[$index] ++;
                $j;
                for ($j = $i; $j >= 0 && $temp < $this->data[$index][$j]; $j--)
                {
                    $this->data[$index][$j + 1] = $this->data[$index][$j]; // 3 2
                    $this->moveCount[$index] ++;
                    $this->compareCount[$index] ++;
                }
                $this->compareCount[$index] ++;
                $this->data[$index][$j + 1] = $temp; //减多了一次，所以要加回来
                $this->moveCount[$index] ++;
            }
        }
    }

}

$insertion = new Insertion($sourceData);
$insertion->begin();

